﻿using System.Runtime.CompilerServices;

namespace KinonekoSoftware.CodariaDB.Runtimes.Categories
{
    public sealed class TreeCatalog : Catalog, IHierarchicalObject
    {
        private bool                        _isVisible;
        private string                      _customType;
        private bool                        _useCustomType;
        private ObservableList<TreeCatalog> _children;

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public bool IsRoot() => string.IsNullOrEmpty(ParentID);

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public bool HasParent() => !string.IsNullOrEmpty(ParentID);

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public string GetID() => Id;

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public string GetParentID() => ParentID;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="instance"></param>
        public void AppendChild(IHierarchicalObject instance)
        {
            var instance2 = (TreeCatalog)instance;
            Children.Add(instance2);
        }

        /// <summary>
        /// 
        /// </summary>
        public bool UseCustomType
        {
            get => _useCustomType;
            set
            {
                SetValue(ref _useCustomType, value);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public string CustomType
        {
            get => _customType;
            set => SetValue(ref _customType, value);
        }

        /// <summary>
        /// 
        /// </summary>
        public bool IsVisible
        {
            get => _isVisible;
            set => SetValue(ref _isVisible, value);
        }

        public string      ParentID    { get; set; }
        public int         Index       { get; set; }

        public override string ToString()
        {
            var count = 0;

            if (Children is not null && Children.Count > 0)
            {
                count = Children.Count;
            }
            
            return $"{Name}({count}), hash:{GetHashCode()}, id: {Id}";
        }


        [JsonIgnore]
        [BsonIgnore]
        public ObservableList<TreeCatalog> Children => _children ??= new ObservableList<TreeCatalog>();
    }
}